package com.jimet.property;

import java.sql.ResultSet;
import com.jimet.db.JimetPropertyDB;

public class JimetProperty {

	
	private String TBL_PREFIX;

	private int id = -1;
	private String name;
	private String type;
	
	//ali: only to be used int JimetUser in Jimet.
	private String value;
	
	
	public JimetProperty(String prefix) 
	{
		this.TBL_PREFIX = prefix;
	}


	
	public boolean delete() throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();
		boolean queryOK;
			queryOK = jimdb.deleteProperty(this.TBL_PREFIX,this.getId());
			queryOK = jimdb.deletePropertyValues(this.TBL_PREFIX,this.getId());
		jimdb.disconnect();
		return queryOK;
	}
	
	public void loadByName(String strPropName) throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();	

		
		ResultSet rs = jimdb.getPropsByName(this.TBL_PREFIX ,strPropName);
		while(rs.next())
		{
			this.setId(rs.getInt(1));
			this.setName(rs.getString(2));
			this.setType(rs.getString(3));
		}
		
		jimdb.disconnect();

	}

	public boolean removeFromObj(int objId) throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();
		boolean queryOK;
			queryOK = jimdb.removeProperty(this.TBL_PREFIX,this.getId(), objId);
		jimdb.disconnect();
		return queryOK;
	}
	public boolean store() throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();
		boolean queryOK;
		if(this.id == -1)
			queryOK = jimdb.addProperty(this.TBL_PREFIX, this.getName(), this.getType());
		else
			queryOK = jimdb.updateProperty(this.TBL_PREFIX, this.getId(), this.getName(), this.getType());
		jimdb.disconnect();
		return queryOK;
	}

	public boolean storePropsToObj(int objId) throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();
		boolean queryOK;
			queryOK = jimdb.addPropertyToObj(this.TBL_PREFIX, this.getId(), this.getValue(), objId);
		jimdb.disconnect();
		return queryOK;
	}
	
	public boolean updatePropsToObj(int objId) throws Exception
	{
		JimetPropertyDB jimdb = new JimetPropertyDB();
		boolean queryOK;
			queryOK = jimdb.updatePropertyToObj(this.TBL_PREFIX,  objId, this.getId(), this.getValue());
		jimdb.disconnect();
		return queryOK;
	}
	
	/**
	 * @return Returns the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id The id to set.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return Returns the type.
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type The type to set.
	 */
	public void setType(String type) {
		this.type = type;
	}



	/**
	 * @return Returns the value.
	 */
	public String getValue() {
		return value;
	}



	/**
	 * @param value The value to set.
	 */
	public void setValue(String value) {
		this.value = value;
	}



}
